class Present::PopupLink < Present::Link
  def initialize(label, url, options = {})
    options[:height] ||= 800
    options[:width] ||= 600
    super(label, url, options)

    html[:target] = '_blank'
    html[:onclick] = "w = window.open(this.href, #{window_name.to_json}, #{window_options.to_json}); return false;"
  end

  # name of the popup window. this is what determines whether a new popup reuses an existing one.
  attr_accessor :window_name

  attr_accessor :height # integer (pixels)
  attr_accessor :width # integer (pixels)
  attr_accessor :toolbar # boolean
  attr_accessor :status # boolean
  attr_accessor :scrollbars # boolean
  attr_accessor :resizable # boolean
  attr_accessor :location # boolean
  attr_accessor :menubar # boolean

  protected

  def window_options
    options = [:height, :width, :toolbar, :status, :scrollbars, :resizable, :location, :menubar].inject([]) do |options, option|
      options << "#{option}=#{send(option)}" unless send(option).nil?
      options
    end
    options.join(',') if options
  end
end